package com.yanchangapp.widget.calendar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.RangeWeekView;
import com.yanchangapp.util.CalendarHelper;

/**
 * 范围选择周视图
 * Created by huanghaibin on 2018/9/13.
 */

public class CustomRangeWeekView extends RangeWeekView {

    private int mRadius;
    private int mPadding;

    public CustomRangeWeekView(Context context) {
        super(context);
        mPadding = dipToPx(getContext(), 2);
    }

    @Override
    protected void onPreviewHook() {
        mRadius = Math.min(mItemWidth, mItemHeight) / 13 * 5;
        mSchemePaint.setStyle(Paint.Style.STROKE);
    }


    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, boolean hasScheme,
                                     boolean isSelectedPre, boolean isSelectedNext) {
        int cx = x + mItemWidth / 2;
        int cy = mItemHeight / 2;

        if (isSelectedPre) {
            if (isSelectedNext) {
                canvas.drawRect(x, cy - mRadius, x + mItemWidth, cy + mRadius, mSelectedPaint);
            } else {//最后一个，the last
                canvas.drawRect(x, cy - mRadius, cx, cy + mRadius, mSelectedPaint);
                canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
            }
        } else {
            if (isSelectedNext) {
                canvas.drawRect(cx, cy - mRadius, x + mItemWidth, cy + mRadius, mSelectedPaint);
            }
            canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);

        }
        return false;
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, boolean isSelected) {
        int cx = x + mItemWidth / 2;
        int cy = mItemHeight / 2;
        canvas.drawCircle(cx, mItemHeight - 2 * mPadding, mRadius, mSchemePaint);
    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, boolean hasScheme, boolean isSelected) {
        float baselineY = mTextBaseLine;
        int cx = x + mItemWidth / 2;
        int cy = mItemHeight / 2;
        int top = - mItemHeight / 6;
        boolean isInRange = isInRange(calendar);
        boolean isEnable = !onCalendarIntercept(calendar);

        // 画农历

        //当然可以换成其它对应的画笔就不麻烦，
        if (!CalendarHelper.isAfterToday(calendar)){
            if (calendar.isWeekend()) {
//                mCurMonthTextPaint.setColor(0xFF489dff);
//                mCurMonthLunarTextPaint.setColor(0xFF489dff);
//                mSchemeTextPaint.setColor(0xFF489dff);
//                mSchemeLunarTextPaint.setColor(0xFF489dff);
//                mOtherMonthLunarTextPaint.setColor(0xFF489dff);
//                mOtherMonthTextPaint.setColor(0xFF489dff);
            }else {
                mCurMonthTextPaint.setColor(0xff333333);
                mCurMonthLunarTextPaint.setColor(0xffCFCFCF);
                mSchemeTextPaint.setColor(0xff333333);
                mSchemeLunarTextPaint.setColor(0xffCFCFCF);
                mOtherMonthTextPaint.setColor(0xFFe1e1e1);
                mOtherMonthLunarTextPaint.setColor(0xFFe1e1e1);
            }
        }else {
            mCurMonthTextPaint.setColor(0xffCFCFCF);
            mCurMonthLunarTextPaint.setColor(0xffCFCFCF);
            mSchemeTextPaint.setColor(0xffCFCFCF);
            mSchemeLunarTextPaint.setColor(0xffCFCFCF);
            mOtherMonthTextPaint.setColor(0xffCFCFCF);
            mOtherMonthLunarTextPaint.setColor(0xffCFCFCF);
        }

        if (isSelected) {
            canvas.drawText(String.valueOf(calendar.getDay()),
                    cx,
                    mTextBaseLine + top,
                    mSelectTextPaint);
            canvas.drawText(calendar.getLunar(), cx, mTextBaseLine + mItemHeight/15f, mSelectedLunarTextPaint);
        } else if (hasScheme) {
            canvas.drawText(String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() && isInRange && isEnable? mSchemeTextPaint : mOtherMonthTextPaint);

        } else {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() && isInRange && isEnable? mCurMonthTextPaint : mOtherMonthTextPaint);
            // 绘制农历
            canvas.drawText(calendar.getLunar(), cx, mTextBaseLine+ mItemHeight/15f,
                    calendar.isCurrentDay() ? mCurDayLunarTextPaint :
                            calendar.isCurrentMonth() && isInRange && isEnable?
                                    mCurMonthLunarTextPaint : mOtherMonthLunarTextPaint);
        }
    }

    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
